/*
 * Copyright 2023-present by Damon All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"context"
	"github.com/bytedance/sonic"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/app/server/registry"
	"github.com/cloudwego/hertz/pkg/app/server/render"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/hertz-contrib/obs-opentelemetry/provider"
	hertztracing "github.com/hertz-contrib/obs-opentelemetry/tracing"
	"github.com/hertz-contrib/registry/nacos"
	"primus/order-service/router"
	"primus/pkg/constants"
	nacosCli "primus/pkg/nacos"
	"primus/pkg/util"
)

/*
* @author Damon
* @date   2023/5/5 11:44
 */

var logFile = "/Users/damon/data/order-service/logs/"

func main() {
	p := provider.NewOpenTelemetryProvider(
		provider.WithServiceName(constants.OrderServiceName),
		provider.WithExportEndpoint("121.37.173.206:4317"), //("localhost:4317"),
		provider.WithInsecure(),
	)

	defer func(ctx context.Context, p provider.OtelProvider) {
		_ = p.Shutdown(ctx)
	}(context.Background(), p)

	hlog.SetOutput(util.NewLogger(logFile))
	hlog.SetLevel(hlog.LevelInfo)

	render.ResetJSONMarshal(sonic.ConfigStd.Marshal)
	conf, err := nacosCli.NewNacosConfig("121.37.173.206", 8848, "")
	if err != nil {
		hlog.Error(err)
	}

	tracer, cfg := hertztracing.NewServerTracer()
	h := server.Default(
		tracer,
		server.WithHostPorts(":10001"),
		server.WithRegistry(nacos.NewNacosRegistry(conf), &registry.Info{
			ServiceName: constants.OrderServiceName,
			Addr:        utils.NewNetAddr("tcp", util.GetIpAddr()+":10001"),
			Weight:      10,
			Tags:        nil,
		}),
	)
	h.Use(hertztracing.ServerMiddleware(cfg))

	router.Init(h)
	h.Spin()

}
